package com.brillilab.starter.controller.aio.space;

import com.brillilab.common.constant.ResultEnum;
import com.brillilab.common.entity.ResponseVo;
import com.brillilab.common.exceptions.BrillilabException;
import com.brillilab.domain.enums.common.IsDeleteEnum;
import com.brillilab.domain.enums.lab.LabMemberPowerEnum;
import com.brillilab.domain.po.space.Room;
import com.brillilab.domain.vo.space.RoomAdd;
import com.brillilab.domain.vo.space.SpaceSearchVo;
import com.brillilab.domain.vo.user.UserInfoVo;
import com.brillilab.service.core.space.IRoomService;
import com.brillilab.service.logic.space.RoomLogic;
import com.brillilab.service.logic.user.UsersLogic;
import com.brillilab.starter.aspect.AspectLog;
import com.brillilab.starter.constant.StarterConstant;
import com.brillilab.starter.handler.RequestPower;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author wuzhiyu
 * @Title: Room
 * @ProjectName brill_lab
 * @Description: 空间管理-房间
 * @date 2018/11/139:24
 */
@RestController
@RequestMapping("/api/spaceManage/room")
public class RoomController {
    @Autowired
    private RoomLogic roomLogic;
    @Autowired
    private UsersLogic usersLogic;
    @Autowired
    private IRoomService roomService;

    @AspectLog
    @RequestPower(value = LabMemberPowerEnum.reagent)
    @PostMapping(value = "/add")
    public ResponseVo add(@RequestBody RoomAdd room, @RequestHeader(value = StarterConstant.TOKEN) String token) {
        if (StringUtils.isBlank(token)) {
            throw new BrillilabException(ResultEnum.USER_NOT_LOGIN);
        }
        Assert.notNull(room, "数据为空");
        Assert.isTrue(!StringUtils.isBlank(room.getName()), "房间名为空");
        UserInfoVo info = usersLogic.getUserByToken(token);
        Long roomId = roomLogic.addRoom(room, info.getLabId());
        return ResponseVo.successMap("roomId", roomId);
    }

    @GetMapping("/list")
    public ResponseVo list(@RequestHeader(value = StarterConstant.TOKEN) String token) {
        UserInfoVo info = usersLogic.getUserByToken(token);
        List<Room> list = roomService.list(info.getLabId());
        return ResponseVo.success(list);
    }

    @AspectLog
    @GetMapping("/edit")
    @RequestPower(value = LabMemberPowerEnum.reagent)
    public ResponseVo edit(Long id, String name, @RequestHeader(value = StarterConstant.TOKEN) String token) {
        if (StringUtils.isBlank(token)) {
            throw new BrillilabException(ResultEnum.USER_NOT_LOGIN);
        }
        Assert.notNull(id, "id为空");
        Assert.isTrue(!StringUtils.isBlank(name), "房间名为空");
        UserInfoVo info = usersLogic.getUserByToken(token);
        Room room = roomService.get(id);
       Assert.notNull(room,"房间不存在");
       Assert.isTrue(IsDeleteEnum.NO.getValue().equals(room.getIsDelete()), "房间不存在");
        if (!room.getLabId().equals(info.getLabId())) {
            throw new BrillilabException(ResultEnum.USER_NOT_POWER);
        }
        return roomLogic.update(id, name) ? ResponseVo.success() : ResponseVo.failure();
    }

    @AspectLog
    @DeleteMapping("/delete")
    @RequestPower(value = LabMemberPowerEnum.reagent)
    public ResponseVo delete(Long id, @RequestHeader(value = StarterConstant.TOKEN) String token) {
        if (StringUtils.isBlank(token)) {
            throw new BrillilabException(ResultEnum.USER_NOT_LOGIN);
        }
        Assert.notNull(id, "id为空");
        Room room = roomService.get(id);
        Assert.notNull(room, "数据不存在");
        UserInfoVo info = usersLogic.getUserByToken(token);
        if (!room.getLabId().equals(info.getLabId())) {
            throw new BrillilabException(ResultEnum.USER_NOT_POWER);
        }
        return roomLogic.delete(id) ? ResponseVo.success() : ResponseVo.failure();
    }

    @GetMapping("/search")
    public ResponseVo search(Integer zone, Long reagentTypeId, Long labMemberId, @RequestHeader(value = StarterConstant.TOKEN) String token) {
        Assert.isTrue(zone!=null||reagentTypeId!=null||!Integer.valueOf(0).equals(labMemberId),"请选择搜索条件");
        if (StringUtils.isBlank(token)) {
            throw new BrillilabException(ResultEnum.USER_NOT_LOGIN);
        }
        if (zone == null && reagentTypeId == null && labMemberId == null) {
            throw new BrillilabException(ResultEnum.REQUEST_PARAM_LACK);
        }
        UserInfoVo info = usersLogic.getUserByToken(token);
        SpaceSearchVo searchVo = roomService.searchSpace(info.getLabId(), zone, reagentTypeId, labMemberId);
        return ResponseVo.success(searchVo);
    }
}
